package database

import (
	"fmt"
	"sg-backend/global"
	"sg-backend/model/paylog"
	"sg-backend/model/user"
	"sg-backend/utils"
)

func InitDb() {

	// 初始化一个管理员账户
	var _user = user.User{Id: 1}
	_u := global.DB.First(&_user)
	if _u.RowsAffected == 0 {

		// 管理用户不存在，创建默认用户
		var username = "admin"
		var password = "123456"
		var nickName = "admin"
		var email = "admin@sgdev.cn"
		var mobile = "13535398904"
		var avatar = "/media/avatar/default.png"
		var passwordSalt = utils.GetRandomString(16)
		var encryptPassword = utils.SHAEncrypt(password + passwordSalt)

		// 创建用户
		adminUser := user.User{
			Id:           1,
			Username:     username,
			Nickname:     nickName,
			Email:        email,
			Mobile:       mobile,
			Avatar:       avatar,
			Password:     encryptPassword,
			PasswordSalt: passwordSalt,
			IsActive:     true,
			IsSuperuser:  true,
			IsFirstLogin: false,
		}
		result := global.DB.Create(&adminUser)
		if result.Error != nil {
			global.SLog.Errorf("初始化管理员账户：admin(123456)失败! %s", result.Error)
			fmt.Printf("初始化管理员账户：admin(123456)失败! %s\n", result.Error)
		} else {
			global.SLog.Infof("初始化管理员账户：admin(123456)成功!")
			fmt.Println("初始化管理员账户：admin(123456)成功!")
		}
	}

	// 用户初始化完成，创建初始化分类
	// 1，支出分类
	// 仅在无数据时初始化
	var payType = paylog.PayType{UserId: 1}
	payTypeCheck := global.DB.First(&payType)
	if payTypeCheck.RowsAffected == 0 {

		// 此用户没有支出分类，需要进行初始化
		payTypeList := []paylog.PayType{
			{Name: "吃喝", Icon: "a-008_mifenmiantiao.png", UserId: 1},
			{Name: "购物", Icon: "gouwuche.png", UserId: 1},
			{Name: "交通", Icon: "a-013_chuzuche.png", UserId: 1},
			{Name: "通讯", Icon: "bianmindianhua.png", UserId: 1},
			{Name: "医疗", Icon: "a-013_xinjianliucheng.png", UserId: 1},
			{Name: "家用", Icon: "a-013_gongsidizhi.png", UserId: 1},
			{Name: "学习", Icon: "a-013_yuanchengbangong.png", UserId: 1},
			{Name: "社交", Icon: "a-013_youxi.png", UserId: 1},
			{Name: "美容", Icon: "a-013_liwufuli.png", UserId: 1},
			{Name: "其他", Icon: "a-013_gengduo-37.png", UserId: 1},
		}
		cr := global.DB.Create(&payTypeList)
		if cr.Error != nil {
			global.SLog.Errorf("初始化支出分类失败！%s", cr.Error)
			fmt.Printf("初始化支出分类失败！%s \n", cr.Error)
		}
	}

	// 2，收入分类
	var incomeType = paylog.IncomeType{UserId: 1}
	incomeTypeCheck := global.DB.First(&incomeType)
	if incomeTypeCheck.RowsAffected == 0 {

		// 此用户没有收入分类，需要进行初始化
		incomeTypeList := []paylog.IncomeType{
			{Name: "工资", Icon: "renminbi_1.png", UserId: 1},
			{Name: "兼职", Icon: "a-013_qingjiashenqing-21.png", UserId: 1},
			{Name: "转入", Icon: "ershoushichang.png", UserId: 1},
			{Name: "理财", Icon: "kehujixi.png", UserId: 1},
			{Name: "捡的", Icon: "moneybag.png", UserId: 1},
			{Name: "其他", Icon: "shengyizhuanrang.png", UserId: 1},
		}
		cr := global.DB.Create(&incomeTypeList)
		if cr.Error != nil {
			global.SLog.Errorf("初始化收入分类失败！%s", cr.Error)
			fmt.Printf("初始化收入分类失败！%s \n", cr.Error)
		}
	}

	// 3，家庭成员
	var familyType = paylog.FamilyType{UserId: 1}
	familyTypeCheck := global.DB.First(&familyType)
	if familyTypeCheck.RowsAffected == 0 {

		// 此用户没有家庭成员分类，需要进行初始化
		familyTypeList := []paylog.FamilyType{
			{Name: "自己", Icon: "nanshi_1.png", UserId: 1},
			{Name: "家庭", Icon: "jiaren.png", UserId: 1},
			{Name: "配偶", Icon: "nvshi_1.png", UserId: 1},
			{Name: "儿女", Icon: "haizi.png", UserId: 1},
			{Name: "父母", Icon: "037-parents.png", UserId: 1},
			{Name: "其他", Icon: "shengyizhuanrang.png", UserId: 1},
		}
		cr := global.DB.Create(&familyTypeList)
		if cr.Error != nil {
			global.SLog.Errorf("初始化家庭成员失败！%s", cr.Error)
			fmt.Printf("初始化家庭成员失败！%s \n", cr.Error)
		}
	}
	// 初始化菜单
	var _menu = user.Menu{Id: 1}
	var _m = global.DB.First(&_menu)
	if _m.RowsAffected == 0 {

		// 初始数据获取不到，初始化管理菜单
		menus := []*user.Menu{
			{Id: 1, Title: "系统面板", Url: "/user", Icon: "SettingOutlined", Pid: 0, OrderNum: 9999, PrivilegeName: "User"},
			{Id: 2, Title: "用户管理", Url: "/user/list", Pid: 1, OrderNum: 1000, PrivilegeName: "UserList"},
			{Id: 3, Title: "菜单管理", Url: "/user/menu", Pid: 1, OrderNum: 1000, PrivilegeName: "UserMenu"},
			{Id: 4, Title: "附件管理", Url: "/user/files", Pid: 1, OrderNum: 1000, PrivilegeName: "UserFiles"},
			{Id: 5, Title: "操作日志", Url: "/user/logs", Pid: 1, OrderNum: 1000, PrivilegeName: "UserLogs"},
			{Id: 6, Title: "首页", Url: "/", Icon: "DashboardOutlined", Pid: 0, OrderNum: 1, PrivilegeName: "Home"},
			{Id: 7, Title: "支出", Url: "/payment", Icon: "MinusCircleOutlined", Pid: 0, OrderNum: 1000, PrivilegeName: "Payment"},
			{Id: 8, Title: "收入", Url: "/income", Icon: "PlusCircleOutlined", Pid: 0, OrderNum: 1000, PrivilegeName: "Income"},
			{Id: 9, Title: "借出", Url: "/lend", Icon: "MinusCircleOutlined", Pid: 0, OrderNum: 1000, PrivilegeName: "Lend"},
			{Id: 10, Title: "借入", Url: "/owe", Icon: "PlusCircleOutlined", Pid: 0, OrderNum: 1000, PrivilegeName: "Owe"},
		}
		r := global.DB.Create(menus)
		if r.Error != nil {
			global.SLog.Errorf("初始化菜单失败！%s", r.Error)
		} else {
			global.SLog.Infof("初始化菜单数据成功！成功添加：%s行初始菜单。", r.RowsAffected)
		}
	}
}
